<?php
require('includes/top.php');

if($_POST['indnew']==3 || $_POST['indnew']==4)
{

if(!isset($_POST['addgrpemp']))
{
$sizeof = 0;
}
else
{
$sizeof = $_POST['addgrpemp'];
}


for ($i = 0; $i <=$sizeof; $i++) {



$empno = $_POST["empno".$i.""];
$emp_fname = $_POST["emp_fname".$i.""];
$emp_lname = $_POST["emp_lname".$i.""];
$emp_desn = $_POST["emp_desn".$i.""];
$emp_telno = $_POST["emp_telno".$i.""];
$emp_address = $_POST["emp_address".$i.""];
$emp_email = $_POST["emp_email".$i.""];
$emp_password = $_POST["empno".$i.""];
$emp_name = $_POST["empno".$i.""];
$emp_doj=$_POST["docgdate".$i.""];
$depot = $_POST["depot".$i.""];

$division = $_POST["division".$i.""];
$region = $_POST["region".$i.""];

if($_POST["laptop_model".$i.""]=="0")
{
$laptop_model = $_POST["laptop_model1".$i.""];
}
else
{
$laptop_model = $_POST["laptop_model".$i.""];
}
foreach($laptop_acc as $data)
{
$a .=  $data.',';
}


$laptop_acc = $a;

$depot_location = $_POST["depot_location"];
$dispatch_location_address = $_POST["dispatch_location_address"];
$dispatch_location = $_POST["dispatch_location"];
$dispatch_date = $_POST["doc3"];
$date = date("d-m-y");

$emp_oldno = $_POST["emp_oldno".$i.""];

$query = "SELECT * FROM users WHERE `user_no` = '".$empno."'";
$result = mysql_query($query);
if ( mysql_num_rows ( $result )> 0 )
{
    header("location:".$_REQUEST['PageName']."&aexit=1");
	die();
}
else
 {
 if($emp_oldno=="") 
{ 
$sql="INSERT INTO users(`user_groups_id`,`user_no`,`user_firstname`,`user_lastname`,`user_telno`,`user_address`,`user_email_address`,`user_password`,`user_name`,`user_designation`,`depot`,`date`,`user_doj`) VALUES ('1','$empno','$emp_fname','$emp_lname','$emp_telno','$emp_address','$emp_email','$emp_password','$emp_name','$emp_desn','$depot','$date','$emp_doj')";


// recipients
$to  = $emp_email; // 
// subject
$subject = 'Login Details';

// message
$message = '
<html>
<head>
  <title>Following is your Login Details:</title>
</head>
<body>
  <p><strong>Following is your Login Details:</strong></p>
  <table>
    <tr>
      <td>Username:</td><td>'.$empno.'</td>
    </tr>
    <tr>
      <td>Password:</td><td>'.$empno.'</td>
    </tr>
  </table>
</body>
</html>
';

// To send HTML mail, the Content-type header must be set
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

// Additional headers
$headers .= 'To:' . $to . "\r\n";
	$headers .= 'From: no-reply@laptrak.com' . "\r\n";

	// Mail it
	mail($to, $subject, $message, $headers);


$result=mysql_query($sql);
 
 
$user_id = mysql_insert_id();



$modelname_array = explode('-',$laptop_model);

$sql8 = "select laptop_id from laptop_details where laptop_name='".trim($modelname_array[0])."' and laptop_model='".trim($modelname_array[1])."' and serial_no='".trim($modelname_array[2])."' and status!=0";
$result8 = mysql_query($sql8);
while($row8 = mysql_fetch_row($result8))
{
  $minid = $row8[0];
}


$sql10 = "select serial_no from laptop_details where laptop_id='".trim($minid)."'";
$result10 = mysql_query($sql10);
$row10 = mysql_fetch_row($result10);
$serial_no = $row10[0];

$sql9="UPDATE laptop_details SET `status` = '0' WHERE laptop_id = '".trim($minid)."'";
$result9=mysql_query($sql9);

if($laptop_model=="0" || $laptop_model=="")
{

$sql1="INSERT INTO pending_request(`laptop_id`,`user_id`,`division`,`region`,`laptopmodel`,`depot_location`,`dispatch_location`,`dispatch_date`,`serial_no`,`date`,`user_name`,`dispatch_location_add`) VALUES ('".trim($minid)."','$user_id','$division','$region','".trim($modelname_array[1])."','$depot_location','$dispatch_location','$dispatch_date','$serial_no','$date','".$_SESSION['user_name']."','$dispatch_location_address')";

$result1=mysql_query($sql1);

}
else
{
$sql1="INSERT INTO laptop(`laptop_id`,`user_id`,`division`,`region`,`laptopmodel`,`depot_location`,`dispatch_location`,`dispatch_date`,`serial_no`,`date`,`user_name`,`dispatch_location_add`) VALUES ('".trim($minid)."','$user_id','$division','$region','".trim($modelname_array[1])."','$depot_location','$dispatch_location','$dispatch_date','$serial_no','$date','".$_SESSION['user_name']."','$dispatch_location_address')";
$result1=mysql_query($sql1);

}

if($result!=1)
{
 header("location:".$_REQUEST['PageName']."&aexit=1");
}
 
 }
 else
{
$query1 = "SELECT user_id FROM users WHERE `user_no` = '{$emp_oldno}'";
$result2 = mysql_query($query1);
if (mysql_num_rows ( $result2)!=1)
{
    header("location:".$_REQUEST['PageName']."&nexit=1");
	die();
}
else
{
$row = mysql_fetch_row($result2);
$uno = $row[0];



$modelname_array = explode('-',$laptop_model);

$sql8 = "select laptop_id from laptop_details where laptop_name='".trim($modelname_array[0])."' and laptop_model='".trim($modelname_array[1])."' and serial_no='".trim($modelname_array[2])."' and status!=0";
$result8 = mysql_query($sql8);
while($row8 = mysql_fetch_row($result8))
{
$minid .= $row8[0];
}


$sql10 = "select serial_no from laptop_details where laptop_id='".trim($minid)."'";
$result10 = mysql_query($sql10);
$row10 = mysql_fetch_row($result10);





$sql9="UPDATE laptop_details SET `status` = '0' WHERE laptop_id = '".trim($minid)."'";
$result9=mysql_query($sql9);


$serial_no = $row10[0];



if($laptop_model=="0" || $laptop_model=="")
{
$sql1="INSERT INTO pending_request(`laptop_id`,`user_id`,`division`,`region`,`laptopmodel`,`depot_location`,`dispatch_location`,`dispatch_date`,`serial_no`,`date`,`user_name`,`dispatch_location_add`) VALUES ('".trim($minid)."','$uno','$division','$region','".trim($modelname_array[1])."','$depot_location','$dispatch_location','$dispatch_date','$serial_no','$date','".$_SESSION['user_name']."','$dispatch_location_address')";

$result3=mysql_query($sql1);

}
else
{
$sql1="INSERT INTO laptop(`laptop_id`,`user_id`,`division`,`region`,`laptopmodel`,`depot_location`,`dispatch_location`,`dispatch_date`,`serial_no`,`date`,`user_name`,`dispatch_location_add`) VALUES ('".trim($minid)."','$uno','$division','$region','".trim($modelname_array[1])."','$depot_location','$dispatch_location','$dispatch_date','$serial_no','$date','".$_SESSION['user_name']."','$dispatch_location_address')";

$result3=mysql_query($sql1);
}
}
}
 
}
header("location:".$_REQUEST['PageName']."&v=success");
}
}
else
{
$empno = $_POST["empno"];
$emp_fname = $_POST["emp_fname"];
$emp_lname = $_POST["emp_lname"];
$emp_desn = $_POST["emp_desn"];
$emp_telno = $_POST["emp_telno"];
$emp_address = $_POST["emp_address"];
$emp_email = $_POST["emp_email"];
$emp_password = $_POST["empno"];
$emp_name = $_POST["empno"];
$emp_doj=$_POST["docdate"];
$depot = $_POST["depot"];
//echo $_POST["laptop_model"]; 

$division = $_POST["division"];
$region = $_POST["region"];
if($_POST["laptop_model"]=="0")
{
   $laptop_model = $_POST["laptop_model1"];
}
else
{
$laptop_model = $_POST["laptop_model"];

}
foreach($laptop_acc as $data)
{
$a .=  $data.',';
}


$laptop_acc = $a;
$depot_location = $_POST["depot_location"];
$dispatch_location_address = $_POST["dispatch_location_address"];
$dispatch_location = $_POST["dispatch_location"];
$dispatch_date = $_POST["doc3"];

$emp_oldno = $_POST["emp_oldno"];

$date = date("d-m-y");

$query = "SELECT * FROM users WHERE `user_no` = '{$empno}'";
$result = mysql_query($query);
if ( mysql_num_rows ( $result )> 0 )
{
    header("location:".$_REQUEST['PageName']."&aexit=1");
	die();
}
else
 {
 
//ENter indiviual new laptop
if($emp_oldno=="") 
{ 
$sql="INSERT INTO users(`user_groups_id`,`user_no`,`user_firstname`,`user_lastname`,`user_telno`,`user_address`,`user_email_address`,`user_password`,`user_name`,`user_designation`,`depot`,`date`,`user_doj`) VALUES ('1','$empno','$emp_fname','$emp_lname','$emp_telno','$emp_address','$emp_email','$emp_password','$emp_name','$emp_desn','$depot','$date','$emp_doj')";

$result=mysql_query($sql);

$user_id = mysql_insert_id();




$modelname_array = explode('-',$laptop_model);
 

$sql8 = "select laptop_id from laptop_details where laptop_name='".trim($modelname_array[0])."' and laptop_model='".trim($modelname_array[1])."' and serial_no='".trim($modelname_array[2])."' and status!=0";
$result8 = mysql_query($sql8);
while($row8 = mysql_fetch_row($result8))
{
$minid .= $row8[0];
}


$sql10 = "select serial_no from laptop_details where laptop_id='".trim($minid)."'";
$result10 = mysql_query($sql10);
$row10 = mysql_fetch_row($result10);





$sql9="UPDATE laptop_details SET `status` = '0' WHERE laptop_id = '".trim($minid)."'";
$result9=mysql_query($sql9);

$serial_no = $row10[0];




// recipients
$to  = $emp_email; // 
// subject
$subject = 'Login Details';

// message
$message = '
<html>
<head>
  <title>Following is your Login Details:</title>
</head>
<body>
  <p><strong>Following is your Login Details:</strong></p>
  <table>
    <tr>
      <td>Username:</td><td>'.$empno.'</td>
    </tr>
    <tr>
      <td>Password:</td><td>'.$empno.'</td>
    </tr>
  </table>
</body>
</html>
';

// To send HTML mail, the Content-type header must be set
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

// Additional headers
$headers .= 'To:' . $to . "\r\n";
$headers .= 'From: no-reply@laptrak.com' . "\r\n";

// Mail it
mail($to, $subject, $message, $headers);
//echo $dispatch_location_address;
//echo $laptop_model;
 

//if($laptop_model=="0")
if($laptop_model=="0" || $laptop_model=="")
{

$sql1="INSERT INTO pending_request(`laptop_id`,`user_id`,`division`,`region`,`laptopmodel`,`depot_location`,`dispatch_location`,`dispatch_date`,`serial_no`,`date`,`user_name`,`dispatch_location_add`) VALUES ('".trim($minid)."','$user_id','$division','$region','".trim($modelname_array[1])."','$depot_location','$dispatch_location','$dispatch_date','$serial_no','$date','".$_SESSION['user_name']."','$dispatch_location_address')";

$result1=mysql_query($sql1);
}
else
{
$sql1="INSERT INTO laptop(`laptop_id`,`user_id`,`division`,`region`,`laptopmodel`,`depot_location`,`dispatch_location`,`dispatch_date`,`serial_no`,`date`,`user_name`,`dispatch_location_add`) VALUES ('".trim($minid)."','$user_id','$division','$region','".trim($modelname_array[1])."','$depot_location','$dispatch_location','$dispatch_date','$serial_no','$date','".$_SESSION['user_name']."','$dispatch_location_address')";

$result1=mysql_query($sql1);
$query10=mysql_query("Select * from laptop_details where serial_no = '".$serial_no."'");
$result_query10=mysql_fetch_array($query10);
//changed
if($result_query10['laptop_condition']!=0)
	{	
		$sql4=mysql_query("Select * from used_laptop_acc");
		while($row4=mysql_fetch_array($sql4))
			{
		$sql5="INSERT INTO laptop_acc_details(`user_id`,`accessories_id`) VALUES ('$user_id','".$row4['acc_id']."')";
		$result5=mysql_query($sql5);
			}
		$sql6=mysql_query("Select * from used_accessories_data where laptop_id=".trim($minid));
		while($row6=mysql_fetch_array($sql6))
			{
			if($row6['accessories_name']=="Data card")
			{			
		$sql10="INSERT INTO accessories(`accessories_id`,`user_id`,`accessories_name`,`company_name`,`division`,`region`,`depot_location`,`dispatch_location`,`card_no`,`date`,`user_name`) VALUES ('".$row6['accessoriesdata_id']."','$user_id','".$row6['accessories_name']."','".$row6['company_name']."','$division','$region','$depot_location','$dispatch_location','".$row6['card_no']."','$date','".$_SESSION['user_name']."')";
		$result10=mysql_query($sql10);
		$sql9 = "Update data_card set `status`='1' where card_no=".$row6['card_no'];
	$result9 = mysql_query($sql9);
			}
			else
			{
				$sql10="INSERT INTO accessories(`accessories_id`,`user_id`,`accessories_name`,`division`,`region`,`depot_location`,`dispatch_location`,`date`,`user_name`) VALUES ('".$row6['accessoriesdata_id']."','$user_id','".$row6['accessories_name']."','$division','$region','$depot_location','$dispatch_location','$date','".$_SESSION['user_name']."')";
		$result10=mysql_query($sql10);
			}

			}
	}
	else {
$sql4=mysql_query("Select * from laptop_acc");
	while($row4=mysql_fetch_array($sql4))
		{
		$sql5="INSERT INTO laptop_acc_details(`user_id`,`accessories_id`) VALUES ('$user_id','".$row4['acc_id']."')";
		$result5=mysql_query($sql5);
		}
	}
}



if($result!=1)
{
 header("location:".$_REQUEST['PageName']."&aexit=1");
}
}
else
{
$query1 = "SELECT user_id FROM users WHERE `user_no` = '{$emp_oldno}'";
$result2 = mysql_query($query1);
if (mysql_num_rows ( $result2)!=1)
{
    header("location:".$_REQUEST['PageName']."&nexit=1");
	die();
}
else
{
$row = mysql_fetch_row($result2);
$uno = $row[0];



$modelname_array = explode('-',$laptop_model);

$sql8 = "select laptop_id from laptop_details where laptop_name='".trim($modelname_array[0])."' and laptop_model='".trim($modelname_array[1])."' and serial_no='".trim($modelname_array[2])."' and status!=0";
$result8 = mysql_query($sql8);
while($row8 = mysql_fetch_row($result8))
{
$minid .= $row8[0];
}


$sql10 = "select serial_no from laptop_details where laptop_id='".trim($minid)."'";
$result10 = mysql_query($sql10);
$row10 = mysql_fetch_row($result10);





$sql9="UPDATE laptop_details SET `status` = '0' WHERE laptop_id = '".trim($minid)."'";
$result9=mysql_query($sql9);


$serial_no = $row10[0];


if($laptop_model=="0" || $laptop_model=="")
{
$sql1="INSERT INTO pending_request(`laptop_id`,`user_id`,`division`,`region`,`laptopmodel`,`depot_location`,`dispatch_location`,`dispatch_date`,`serial_no`,,`date`,`user_name`,`dispatch_location_add`) VALUES ('".trim($minid)."','$uno','$division','$region','".trim($modelname_array[1])."','$depot_location','$dispatch_location','$dispatch_date','$serial_no','$date','".$_SESSION['user_name']."','$dispatch_location_address')";

$result3=mysql_query($sql1);
}
else
{
$sql1="INSERT INTO laptop(`laptop_id`,`user_id`,`division`,`region`,`laptopmodel`,`depot_location`,`dispatch_location`,`dispatch_date`,`serial_no`,`date`,`user_name`,`dispatch_location_add`) VALUES ('".trim($minid)."','$uno','$division','$region','".trim($modelname_array[1])."','$depot_location','$dispatch_location','$dispatch_date','$serial_no','$date','".$_SESSION['user_name']."','$dispatch_location_address')";

$result3=mysql_query($sql1);
//Insert the laptop accessories automatically
	if($result_query10['laptop_condition']!=0)
	{	
		$sql4=mysql_query("Select * from used_laptop_acc");
		while($row4=mysql_fetch_array($sql4))
			{
		$sql5="INSERT INTO laptop_acc_details(`user_id`,`accessories_id`) VALUES ('$user_id','".$row4['acc_id']."')";
		$result5=mysql_query($sql5);
			}
		$sql6=mysql_query("Select * from used_accessories_data where laptop_id=".trim($minid));
		while($row6=mysql_fetch_array($sql6))
			{
			if($row6['accessories_name']=="Data card")
			{			
		$sql10="INSERT INTO accessories(`accessories_id`,`user_id`,`accessories_name`,`company_name`,`division`,`region`,`depot_location`,`dispatch_location`,`card_no`,`date`,`user_name`) VALUES ('".$row6['accessoriesdata_id']."','$user_id','".$row6['accessories_name']."','".$row6['company_name']."','$division','$region','$depot_location','$dispatch_location','".$row6['card_no']."','$date','".$_SESSION['user_name']."')";
		$result10=mysql_query($sql10);
		$sql9 = "Update data_card set `status`='1' where card_no=".$row6['card_no'];
	$result9 = mysql_query($sql9);
	
			}
			else
			{
				$sql10="INSERT INTO accessories(`accessories_id`,`user_id`,`accessories_name`,`division`,`region`,`depot_location`,`dispatch_location`,`date`,`user_name`) VALUES ('".$row6['accessoriesdata_id']."','$user_id','".$row6['accessories_name']."','$division','$region','$depot_location','$dispatch_location','$date','".$_SESSION['user_name']."')";
		$result10=mysql_query($sql10);
			}

			}
	}
	else {
$sql4=mysql_query("Select * from laptop_acc");
	while($row4=mysql_fetch_array($sql4))
		{
		$sql5="INSERT INTO laptop_acc_details(`user_id`,`accessories_id`) VALUES ('$user_id','".$row4['acc_id']."')";
		$result5=mysql_query($sql5);
		}
	}
		
}
}
}

}

header("location:".$_REQUEST['PageName']."&v=success");
}
?>
